package com.doyoulike.util;

import cn.hutool.core.util.StrUtil;
import com.doyoulike.entity.DataBaseColumn;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.util.List;

/**
 *
 * @author 独舟
 * @description 字段数据类型工具类
 * @version 2020-03-21
 */
public class ColumnUtil {

    /**
     * 分隔符
     */
    private static final char SEPARATOR = '_';

    /**
     * 获取 MySQL 对应 Java 数据类型配置信息
     */
    public static PropertiesConfiguration getConfig() {
        try {
            return new PropertiesConfiguration("gen/mysql_java_datatype.properties");
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取 MySQL 对应 MyBatis 数据类型配置信息
     */
    public static PropertiesConfiguration getMySQLMyBatisDataTypeConfig() {
        try {
            return new PropertiesConfiguration("gen/mysql_mybatis_datatype.properties");
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取 添加时 排除的字段配置信息
     */
    public static PropertiesConfiguration getExcludeFieldConfig() {
        try {
            return new PropertiesConfiguration("gen/add_exclude_field.properties");
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 转换mysql数据类型为java数据类型
     * @param type
     * @return
     */
    public static String cloToJava(String type){
        Configuration config = getConfig();
        return config.getString(type,null);
    }

    /**
     * 转换mysql数据类型为mybatis数据类型
     * @param type
     * @return
     */
    public static String cloToMyBatis(String type){
        Configuration config = getMySQLMyBatisDataTypeConfig();
        return config.getString(type,null);
    }

    /**
     * 判断是否是排除的字段
     */
    public static boolean isAddExcludeField(String mysqlFieldName){
        Configuration config = getExcludeFieldConfig();
        String addExcludeFieldStr = config.getString("addExcludeField", null);
        List<String> addExcludeFieldList = StrUtil.splitTrim(addExcludeFieldStr, ";");
        return addExcludeFieldList.contains(mysqlFieldName);
    }

    /**
     * 判断是否是更新排除的字段
     */
    public static boolean isEditExcludeField(String mysqlFieldName){
        Configuration config = getExcludeFieldConfig();
        String addExcludeFieldStr = config.getString("editExcludeField", null);
        List<String> addExcludeFieldList = StrUtil.splitTrim(addExcludeFieldStr, ";");
        return addExcludeFieldList.contains(mysqlFieldName);
    }

    /**
     * 驼峰命名法工具（首字母小写）
     * @return toCamelCase(" hello_world ") == "helloWorld"
     */
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    /**
     * 驼峰命名法工具（首字母小写）
     * @return HelloWorld ---> helloWorld
     */
    public static String toCamelCaseFirst(String s) {
        String firstStr = s.substring(0, 1);
        String afterStr = s.substring(1, s.length());
        String result = firstStr.toLowerCase() + afterStr;
        return result;
    }

    /**
     * 驼峰命名法工具（首字母大写）
     * @return toCapitalizeCamelCase("hello_world") == "HelloWorld"
     * toUnderScoreCase("helloWorld") = "hello_world"
     */
    public static String toCapitalizeCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = toCamelCase(s);
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }

    /**
     * 驼峰命名法工具（首字母大写）
     * @return toCapitalizeCamelCase("hello_world") == "HelloWorld"
     * toUnderScoreCase("helloWorld") = "hello_world"
     */
    public static String toCapitalizeCamelCaseRemovePrefix(String s) {
        if (s == null) {
            return null;
        }
        s = s.substring(s.indexOf("_") + 1);
        s = toCamelCase(s);
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }



    /**
     * 设置Java数据类型、Mybatis数据类型、Java字段名
     * @param columnList
     */
    /*public static void initColumn(List<GenTableColumn> columnList) {
        for (GenTableColumn genTableColumn : columnList) {
            // 设置Java数据类型
            genTableColumn.setJavaType(cloToJava(genTableColumn.getJdbcType()));

            // 设置MyBatis数据类型
            genTableColumn.setJdbcType(cloToMyBatis(genTableColumn.getJdbcType()));

            // 设置Java字段名（驼峰小写）
            genTableColumn.setJavaField(ColumnUtil.toCamelCase(genTableColumn.getName()));

            // 是否查询条件字段，默认设置为否
            genTableColumn.setIsQuery(0);
        }
    }*/

    /**
     * 设置Java数据类型、Mybatis数据类型、Java字段名
     * @param columnList
     */
    public static void initColumn(List<DataBaseColumn> columnList) {
        for (DataBaseColumn column : columnList) {
            // 设置Java数据类型
            column.setJavaType(cloToJava(column.getJdbcType()));

            // 设置MyBatis数据类型
            column.setJdbcType(cloToMyBatis(column.getJdbcType()));

            // 设置Java字段名（驼峰小写）
            column.setJavaField(ColumnUtil.toCamelCase(column.getName()));
        }
    }
}
